<?php

namespace App\Http\Controllers\DingTalk;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\DingTalkMessage;
use App\Models\DingTalkRobotToken;
use App\Models\DingTalkApp;
use App\Models\DingTalkUser;

class RobotController extends Controller
{
    //
    public function index()
    {
        if (post("?")) {
            return DingTalkRobotToken::search(post("search"))

                ->relatedJoin()
                ->getGrid();
        } else {
            return view('DingTalk.RobotController.index', F());
        }
    }

    public function indexmsg()
    {
        return DingTalkMessage::search(post("search"))
            ->where(['dingtalk_message.kind' => 'robot'])
            ->relatedJoin()
            ->getGrid();
    }

    public function addMsg()
    {

        if (post("?")) {
            $data = post('data');
            $userdata = DingTalkUser::where(['id' => $data['userid']])->first();
            $robotToken = DingTalkRobotToken::where(['id' => get("id")])->first();
            $app = DingTalkApp::where(['id' => $robotToken['app_id']])->first();
            $name = $app['name'];
            $robot = app("DT")->$name;
            $msg['msgKey'] = $data['msgKey'];
            $msg['msgParam'] = $data['msgParam'];
            $res = $robot->singleMsg($msg, $userdata['userid']);
            return success(json_encode($res));
        } else {
            $param['selectuser']['data'] = DingTalkUser::get();
            $param['selectuser']['name'] = "data[userid]";
            return view('DingTalk.RobotController.addMsg', $param);
        }
    }

    public function addGroupMsg()
    {
        if (post("?")) {

            $data = post('data');
            $userdata = DingTalkUser::where(['id' => $data['userid']])->first();
            $robotToken = DingTalkRobotToken::where(['id' => get("id")])->first();
            $app = DingTalkApp::where(['id' => $robotToken['app_id']])->first();
            if ($app)
                $name = $app['name'];
            else
                $name = 'robot';
            $robot = app("DT")->$name;
            $msg['msgtype'] = $data['msgKey'];
            $msg[$data['msgKey']] = json_decode($data['msgParam']);

            $res = $robot->setWebHookToken($robotToken['token'])->setSppSecret($robotToken['sign'])->atUserids($userdata['userid'])->sendGroup($msg);
            return success(json_encode($res));
        } else {
            $param['selectuser']['data'] = DingTalkUser::get();
            $param['selectuser']['name'] = "data[userid]";
            return view('DingTalk.RobotController.addGroupMsg', $param);
        }
    }
    public function add()
    {
        if (post("?")) {
            $data = post('data');
            try {
                begin();
                $data['creator_id'] = user()->id;
                DingTalkRobotToken::create(array_filter($data));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }
            return success("保存成功！");
        } else {
            // $param=selectrobot;
            // dd(DingTalkApp::where(["creator_id" => user()->id, 'kind' => '机器人'])->sql());
            $param['selectrobot']['data'] = DingTalkApp::where(['kind' => '机器人'])->get()->toArray();
            $param['selectrobot']['data'][] = ['id' => '', 'name' => '请选择'];
            $param['selectrobot']['name'] = "data[app_id]";
            return view('DingTalk.RobotController.add', $param);
        }
    }
    public function edit()
    {
        return DingTalkMessage::where(["id" => get('id')])->get();
    }

    public function delete()
    {

        try {
            begin();
            DingTalkRobotToken::whereIn("id", post('ids'))
                ->get()
                ->filter(function ($value, $key) {
                    $value->delete();
                });
            commit();
        } catch (\Exception $e) {
            rollback();
            return error($e->getMessage());
        }
        return success("保存成功！");
    }
}
